﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Configuration;

public partial class forgotPassword : System.Web.UI.Page
{
    security baomat = new security();
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    private string ReadTemplate(string username,string newpass)
    {
        string content = String.Empty;
        try
        {
            content = baomat.ReadTemplate(Server.MapPath("template/ForgotMail.htm"));
            content = content.Replace("{0}",username).Replace("{1}",newpass);
        }
        catch (Exception)
        {
            Response.RedirectToRoute("Error");
        }
        return content;
    }
    protected void btnforgotpwd_Click(object sender, EventArgs e)
    {
        try
        {
            if (txtUsername.Text != "")
            {
                using (Up1VNDataContext dc = new Up1VNDataContext())
                {
                    string guidResult = System.Guid.NewGuid().ToString();
                    bool sendmail = false;
                    string Newpass = guidResult.ToString().Replace("-", "").Substring(3, 6);
                    string username=String.Empty;
                    if (txtUsername.Text.Contains("@"))
                    {
                        var find = from u in dc.Users
                                   where u.Users_Email == txtUsername.Text.Trim()
                                   select u;
                        if (find.Count() > 0)
                        {
                            username = find.Single().Users_Name;
                            sendmail = baomat.sendmail(txtUsername.Text, ConfigurationManager.AppSettings["Support"].ToString(), ReadTemplate(username, Newpass), "Lấy lại mật khẩu");
                        }
                        else
                            lblmsg.Text = "Email không đúng.";
                    }
                    else
                    {
                        username = txtUsername.Text.Trim();
                        var find = from u in dc.Users
                                   where u.Users_Name == txtUsername.Text.Trim()
                                   select u;
                        if (find.Count() > 0)
                        {
                            sendmail = baomat.sendmail(find.Single().Users_Email, ConfigurationManager.AppSettings["Support"].ToString(), ReadTemplate(txtUsername.Text, Newpass), "Lấy lại mật khẩu");
                        }
                        else
                            lblmsg.Text = "Tên đăng nhập không đúng.";
                    }
                    if (sendmail)
                    {
                        User U = dc.Users.Single(u => u.Users_Name == username);
                        U.Users_Password =baomat.EncryptPWD(Newpass);
                        dc.SubmitChanges();
                        lblmsg.Text = "Bạn vui lòng kiểm tra địa chỉ email.";
                    }
                }
            }
            else
                lblmsg.Text = "Nhận email hoặc tên đăng nhập.";
        }
        catch (Exception)
        {
        }
    }
}